package com.shadow.demo.data.sys;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.shadow.demo.data.info.mapper.SentenceMapper;
import com.shadow.demo.data.shiro.mapper.UserMapper;
import com.shadow.demo.common.bo.R;
import com.shadow.demo.common.bo.ResultInfo;
import com.shadow.demo.common.util.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@Slf4j
@Order(2)
@RestController
@RequestMapping("health")
public class HealthController {

    private final UserMapper userMapper;
    private final SentenceMapper sentenceMapper;
    private final HealthMapper healthMapper;

    HealthController(
            UserMapper userMapper,
            SentenceMapper sentenceMapper,
            HealthMapper healthMapper
    ) {
        this.userMapper = userMapper;
        this.sentenceMapper = sentenceMapper;
        this.healthMapper = healthMapper;
    }

    @GetMapping("system")
    public R systemHealth() {
        log.info("正常, 系统检查");
        return R.success();
    }

    @DS("master")
    @GetMapping("datasource/demo")
    public R demoDatasource() {
        Long count = userMapper.selectCount(null);
        log.info("正常, demo数据库检查");
        log.info("UserMapper#selectCount, count: {}", count);
        return R.success(count);
    }

    @DS("info")
    @GetMapping("datasource/info")
    public R infoDatasource() {
        Long count = sentenceMapper.selectCount(null);
        log.info("正常, info数据库检查");
        log.info("SentenceMapper#selectCount, count: {}", count);
        return R.success(count);
    }

    @GetMapping("datasource/version")
    public R datasourceVersion() {
        String version = healthMapper.version();
        log.info("数据库版本, version : {}", version);
        return R.success(version);
    }


    @GetMapping("http/baidu")
    public R baiduHttp() {
        log.info("正常, http2baidu");
        ResultInfo resultInfo = HttpUtils.sendGet("https://www.baidu.com/favicon.ico", new HashMap<>());
        if (resultInfo.isResult()) {
            R.success(resultInfo.getData());
        }
        return R.fail();
    }
}
